package com.etong.frame.directive.impl.database;

import java.util.ArrayList;
import java.util.List;

class Field {
	// 名字
	private String name;
	// 类型
	private String type;
	// 值
	private String value;
	// 可替换的
	boolean replaced;
	// 必选的
	boolean required;
	// 占位对象
	private List<Placeholder> placeholders;

	public Field() {
		placeholders = new ArrayList<Placeholder>();
	}

	public boolean isRequired() {
		return required;
	}

	public void setRequired(boolean required) {
		this.required = required;
	}

	public String getName() {
		return name;
	}

	public void setName(String fieldName) {
		this.name = fieldName;
	}

	public String getType() {
		return type;
	}

	public void setType(String jdbcType) {
		this.type = jdbcType;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String fieldValue) {
		this.value = fieldValue;
	}

	public boolean isReplaced() {
		return replaced;
	}

	public void setReplaced(boolean replaced) {
		this.replaced = replaced;
	}

	public List<Placeholder> getPlaceholders() {
		return placeholders;
	}

	public void setPlaceholders(List<Placeholder> placeholders) {
		this.placeholders = placeholders;
	}

	public void createValue(String valuesExpr) {
		List<Placeholder> valuePlaceholders = DatabaseDirective
				.createPlaceholders(valuesExpr);

		Field field = this;
		// set placeholders
		// 字段值中有变量,在build中进行替换
		if (valuePlaceholders.size() > 0) {
			field.setValue(valuesExpr);
			field.setReplaced(true);
			boolean required = false;
			// 如果有一个变量是必须的，则这个字段是必须的
			for (Placeholder placeholder : valuePlaceholders) {
				required |= placeholder.isRequired();
			}
			field.setRequired(required);
			// 将变量添加列表中
			this.placeholders.addAll(valuePlaceholders);
		} else {
			field.setValue(valuesExpr);
			field.setReplaced(false);
		}
	}

}
